package pct.post;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import pct.database.PcServiceImpl;
import pct.manage.StatusManager;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class AccessPosts extends ActionSupport implements SessionAware, Preparable{
	private int selectedTopicID;
	private PcServiceImpl pcService = new PcServiceImpl();
	private Map<String, Object> session;
	public int getSelectedTopicID() {
		return selectedTopicID;
	}

	public void setSelectedTopicID(int selectedTopicID) {
		this.selectedTopicID = selectedTopicID;
	}


	public String execute(){
		// fetch the status manager from session map
		// update its relatedPosts property due to the topic user selects
		StatusManager sm = (StatusManager) session.get("status");
		sm.setRelatedPosts(pcService.getRelatedPosts(selectedTopicID));
		sm.setCurrentTopic(pcService.getCurrentTopic(selectedTopicID));
		session.put("status", sm);
		return SUCCESS;
	}
	

	
	@Override
	public void prepare() throws Exception {
		pcService.connectDB();
		
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;		
		
	}

}
